<?php
require_once("dbcon.php");


$max_version = 3;
$install_sql = array(
array("CREATE TABLE `accounts` (
  `id` int(5) unsigned NOT NULL AUTO_INCREMENT,
  `company_name` varchar(200) NOT NULL,
  `first_name` varchar(200) NOT NULL,
  `last_name` varchar(200) NOT NULL,
  `phone` text NOT NULL,
  `email` text NOT NULL,
  `address` text NOT NULL,
  `Last_Update` datetime NOT NULL,
  `Created_At` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8"),
array("CREATE TABLE `categories` (
  `id` int(5) unsigned NOT NULL AUTO_INCREMENT,
  `parent_id` int(5) unsigned DEFAULT NULL,
  `name` varchar(200) NOT NULL,
  `Created_At` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8",
"CREATE TABLE `products` (
  `id` int(5) unsigned NOT NULL AUTO_INCREMENT,
  `category_id` int(5) unsigned DEFAULT NULL,
  `name` varchar(200) NOT NULL,
  `code` varchar(200) NOT NULL,
  `small_desc` varchar(400) NOT NULL,
  `full_desc` varchar(400) NOT NULL,
  `Created_At` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
",
"CREATE TABLE `products_variants` (
  `id` int(5) unsigned NOT NULL AUTO_INCREMENT,
  `product_id` int(5) unsigned DEFAULT NULL,
  `article` varchar(200) NOT NULL,
  `desc` varchar(400) NOT NULL,
  `price` decimal(10,2) NOT NULL,
  `Created_At` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8"),
array(
"CREATE TABLE `auth` (" .
" `id` int(5) unsigned NOT NULL," .
" `uname` text NOT NULL,".
" `pass` text NOT NULL, ".
" `level` int(5) unsigned NOT NULL, ".
" PRIMARY KEY(`id`)".
") ENGINE=InnoDB DEFAULT CHARSET=utf8" 
)
);
$uninstall_sql = array(
array("DROP TABLE IF EXISTS `accounts`"),
array("DROP TABLE IF EXISTS `categories`",
"DROP TABLE IF EXISTS `products`",
"DROP TABLE IF EXISTS `products_variants`"),
array("DROP TABLE IF EXISTS `auth`")
);

function migrate($ver) {
	global $install_sql;
	for ($i = 0; $i < $ver; $i++) {
		foreach($install_sql[$i] as $sql) {
			mysql_query($sql);
			if (mysql_error()) echo mysql_error();
		}	
	}
}

function demigrate($ver) {
	global $uninstall_sql;
	for ($i = $ver-1; $i >= 0; $i--) {
		foreach($uninstall_sql[$i] as $sql) {
			mysql_query($sql);
			if (mysql_error()) echo mysql_error();
		}	
	}
}

?>
